package core.threads;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class TaskManager {

	private volatile Collection<Future<Void>> tasks;
	
	public TaskManager() {
		tasks = Collections.synchronizedList(new LinkedList<Future<Void>>());
	}
	
	public void addTask(Callable<Void> task) {
		synchronized(tasks) {
			tasks.add(ThreadPool.submitTask(task));
		}
	}
	
	public void waitForAllTasksToBeDone() {
		synchronized(tasks) {
			for (Future<Void> task : tasks) {
				while (!task.isDone());
			}
			tasks.clear();
		}
	}
	
}
